package com.tcwlkj.uuService.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.bumptech.glide.Glide;
import com.makeramen.roundedimageview.RoundedImageView;
import com.tcwlkj.uuService.MyApp;
import com.tcwlkj.uuService.R;
import com.tcwlkj.uuService.model.LastFeedModle;
import com.tcwlkj.uuService.utils.ChcheUtils;
import com.tcwlkj.uuService.utils.GlideCircleTransform;
import com.tcwlkj.uuService.utils.PreferencesUtils;
import com.tcwlkj.uuService.utils.Tools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by Administrator on 2017/10/19 0019.
 */

public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ViewHolder> {
    private ArrayList<LastFeedModle.DataBean> mMsgList;

    public ChatAdapter(ArrayList<LastFeedModle.DataBean> mMsgList){
        this.mMsgList=mMsgList;
    }
    public void updateData(ArrayList<LastFeedModle.DataBean> data) {
        this.mMsgList = data;
        notifyDataSetChanged();
    }

    @Override
    public ChatAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.chat_recyclerview_item, null);
        return new ViewHolder(view);
    }

    long time=0;
    @Override
    public void onBindViewHolder(ChatAdapter.ViewHolder holder, int position) {
        LastFeedModle.DataBean data=mMsgList.get(position);
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date= null;
        try {
            date = sdf1.parse(data.getAddTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Tools.transTime(date.getTime());
        SimpleDateFormat sdf=new SimpleDateFormat("HH:mm");

        holder.tv_time.setText(Tools.transTime(date.getTime()));

        if(position-1>0){
            if(isShortTime(Tools.StringToDate(mMsgList.get(position).getAddTime()).getTime(),Tools.StringToDate(mMsgList.get(position-1).getAddTime()).getTime())){
                holder.tv_time.setVisibility(View.VISIBLE);
            }else {
                holder.tv_time.setVisibility(View.GONE);
            }
        }else {
            holder.tv_time.setVisibility(View.VISIBLE);
        }
        if(data.getIsFeed()==1){//用户
            holder.rl_chat_chat.setVisibility(View.GONE);
            holder.rl_chat_me.setVisibility(View.VISIBLE);
            holder.tv_chat_me.setText(data.getContent());
            if(ChcheUtils.getInstance().getDriverDetailModel()==null){
                Glide.with(MyApp.getContext()).load(R.mipmap.iv_touxiang).error(R.mipmap.iv_touxiang).bitmapTransform(new GlideCircleTransform(MyApp.getContext())).into(holder.iv_me);
            }else {
                Glide.with(MyApp.getContext()).load(ChcheUtils.getInstance().getDriverDetailModel().getPhotos().get(0).getSamllPhoto()).error(R.mipmap.iv_touxiang).bitmapTransform(new GlideCircleTransform(MyApp.getContext())).into(holder.iv_me);
            }
        }else {//客服
            holder.rl_chat_chat.setVisibility(View.VISIBLE);
            holder.rl_chat_me.setVisibility(View.GONE);
            holder.tv_chat_kefu.setText(data.getContent());
            Glide.with(MyApp.getContext()).load(R.mipmap.chat_kefu_touxiang).error(R.mipmap.personal_touxiang).bitmapTransform(new GlideCircleTransform(MyApp.getContext())).into(holder.iv_kefu);
        }
    }

    @Override
    public int getItemCount() {
        return mMsgList.size();
    }
    public static class ViewHolder extends RecyclerView.ViewHolder {

        private RelativeLayout rl_time;
        private LinearLayout rl_chat_chat;
        private RelativeLayout rl_chat_me;
        private TextView tv_chat_kefu;
        private TextView tv_chat_me;
        private ImageView iv_kefu;
        private ImageView iv_me;
        private TextView tv_time;
        public ViewHolder(View itemView) {
            super(itemView);
            rl_time= (RelativeLayout) itemView.findViewById(R.id.rl_time);
            rl_chat_chat= (LinearLayout) itemView.findViewById(R.id.rl_chat_chat);
            rl_chat_me= (RelativeLayout) itemView.findViewById(R.id.rl_chat_me);
            tv_chat_kefu= (TextView) itemView.findViewById(R.id.tv_chat_kefu);
            tv_chat_me= (TextView) itemView.findViewById(R.id.tv_chat_me);
            iv_kefu= (ImageView) itemView.findViewById(R.id.iv_kefu);
            iv_me= (ImageView) itemView.findViewById(R.id.iv_me);
            tv_time= (TextView) itemView.findViewById(R.id.tv_time);
        }
    }
    private boolean isShortTime(double time1,double time2){
        if(time1-time2>300000){
            return true;
        }else{
            return false;
        }
    }

}
